import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

public class BatchFileQueShiDownloader {
    public static void main(String[] args) {
        // 下载文件的URL列表（文件名自动提取）
        // 下载文件的URL列表（文件名自动提取）
        List<String> urls = Arrays.asList(
                "https://pic.vchat-onlie.com/country/flag/ic_united_kingdom.png",
                "https://pic.vchat-onlie.com/country/flag/ic_ukraine.png",
                "https://pic.vchat-onlie.com/country/flag/ic_yemen.png",
                "https://pic.vchat-onlie.com/country/flag/ic_vietnam.png",
                "https://pic.vchat-onlie.com/country/flag/ic_ireland.png",
                "https://pic.vchat-onlie.com/country/flag/ic_finland.png",
                "https://pic.vchat-onlie.com/country/flag/ic_brazil.png",
                "https://pic.vchat-onlie.com/country/flag/ic_argentina.png",
                "https://pic.vchat-onlie.com/country/flag/ic_peru.png",
                "https://pic.vchat-onlie.com/country/flag/ic_venezuela.png",
                "https://pic.vchat-onlie.com/flag/ic_Bolivia.png",
                "https://pic.vchat-onlie.com/flag/ic_Chile.png",
                "https://pic.vchat-onlie.com/flag/ic_Colombia.png",
                "https://pic.vchat-onlie.com/flag/ic_Costa_Rica.png",
                "https://pic.vchat-onlie.com/flag/ic_Cuba.png",
                "https://pic.vchat-onlie.com/flag/ic_Dominica.png",
                "https://pic.vchat-onlie.com/flag/ic_Ecuador.png",
                "https://pic.vchat-onlie.com/flag/ic_El_Salvador.png",
                "https://pic.vchat-onlie.com/flag/ic_Guatemala.png",
                "https://pic.vchat-onlie.com/flag/ic_Honduras.png",
                "https://pic.vchat-onlie.com/flag/ic_Mexico.png",
                "https://pic.vchat-onlie.com/flag/ic_Nicaragua.png",
                "https://pic.vchat-onlie.com/flag/ic_Panama.png",
                "https://pic.vchat-onlie.com/flag/ic_Paraguay.png",
                "https://pic.vchat-onlie.com/flag/ic_Uruguay.png",
                "https://pic.vchat-onlie.com/flag/ic_taiwan.png",
                "https://pic.vchat-onlie.com/flag/ic_Singapore.png",
                "https://pic.vchat-onlie.com/flag/ic_sweden.png",
                "https://pic.vchat-onlie.com/flag/ic_nepal_flag.png",
                "https://pic.vchat-onlie.com/ic_greece_flag.png",
                "https://pic.vchat-onlie.com/country/flag/ic_switzerland.png",
                "https://pic.vchat-onlie.com/flag/ic_belgium.png",
                "https://pic.vchat-onlie.com/flag/ic_Afghanistan.png",
                "https://pic.vchat-onlie.com/country/flag/ic_china.png",
                "https://pic.olamet.net/country/png/nigeria.png",
                "https://pic.olamet.net/country/png/madagascar.png",
                "https://pic.olamet.net/country/png/ghana.png",
                "https://pic.olamet.net/country/png/kenya.png",
                "https://pic.olamet.net/country/png/luxembourg.png",
                "https://pic.olamet.net/country/png/mauritius.png",
                "https://pic.olamet.net/country/png/Republic_of_Belarus.png"
        );


        // 下载目标目录
        String downloadDir = "C:\\Users\\Administrator\\Desktop\\country";

        // 创建下载目录（如果不存在）
        File dir = new File(downloadDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        // 下载文件
        for (String fileUrl : urls) {
            try {
                // 提取文件名
                String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);

                // 创建文件输出流
                File outputFile = new File(downloadDir, fileName);
                if (outputFile.exists()) {
                    System.out.println(fileName + " 已存在，跳过下载。");
                    continue;
                }

                // 下载文件
                System.out.println("正在下载: " + fileName);
                downloadFile(fileUrl, outputFile);
                System.out.println(fileName + " 下载完成。");
                Thread.sleep(1000);
            } catch (Exception e) {
                System.err.println("下载失败: " + fileUrl + " - " + e.getMessage());
            }
        }

        System.out.println("所有文件下载完成！");
    }

    // 下载文件方法
    private static void downloadFile(String fileUrl, File outputFile) throws IOException {
        URL url = new URL(fileUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setConnectTimeout(10000);
        connection.setReadTimeout(10000);

        try (InputStream inputStream = connection.getInputStream();
             FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }
        }
    }
}
